﻿
//***********************************************************************
// * Module:   TbBstFraudCollectHandler
// * 作者:  曹荣芝
// * Modified: 2014/5/25
// * Purpose: Definition of the Class TbBstFraudCollectHandler 业务逻辑处理Handler类
// ***********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XWG.Framework.MSException;
using XWG.Framework.AuthUtility;
using XWG.Framework.AuthEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthBLL;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.JWBSTSPConst;
using System.Web.Script.Serialization;
using System.Data;
using XWG.Framework.Query;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using XWG.JWBST.JWBSTBLL;
using System.Collections;
using JWBSTAPP.CommonClass;
using XWG.Framework;
namespace JWBSTAPP.AjaxServer
{
    /// <summary>
    /// TbBstFraudCollectHandler 的摘要说明
    /// </summary>
    public class TbBstFraudCollectHandler : JWBSTAPP.CommonClass.HttpHandlerBase, System.Web.SessionState.IRequiresSessionState
    {

        #region 添加记录
        /// <summary>
        /// 添加记录
        /// </summary>
        protected void AddRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            try
            {
                TbBstFraudCollect mTbBstFraudCollect = new TbBstFraudCollect();
                mTbBstFraudCollect.Id = Guid.NewGuid().ToString(); //主键
                mTbBstFraudCollect.Receivedatetime = Convert.ToDateTime(context.Request.Form["texReceivedatetime"]); //收到诈骗电话（短信）时间
                mTbBstFraudCollect.Tel = context.Request.Form["texTel"]; //来电显示的号码
                if (!string.IsNullOrEmpty(mTbBstFraudCollect.Tel))
                {
                    string telNum = mTbBstFraudCollect.Tel.Substring(0, 3);
                    string carrieroperator = TbBstFraudCollectBLL.GetName(telNum);
                    //被举报号码所属运营商名称 来电显示号码为非手机号的名称为其他
                    if (!string.IsNullOrEmpty(carrieroperator))
                    {
                        mTbBstFraudCollect.Operatorsname = carrieroperator;
                    }
                    else
                    {
                        mTbBstFraudCollect.Operatorsname = "其他";
                    }
                    string carrieroperatorNum = "0";
                    switch (carrieroperator)
                    {
                        case "中国移动":
                            carrieroperatorNum = "1";
                            break;
                        case "中国联通":
                            carrieroperatorNum = "2";
                            break;
                        case "中国电信":
                            carrieroperatorNum = "3";
                            break;
                        case "":
                            carrieroperatorNum = "0";
                            break;
                        default:
                            break;
                    }
                    mTbBstFraudCollect.Operatorscode = carrieroperatorNum; //被举报号码所属运营商代码 1 移动号码 2 联通 3 电信 0 其他（来电显示号码为非手机号的）
                }
                mTbBstFraudCollect.Fraudtype = Convert.ToDecimal(context.Request.Form["selFraudtype"]); //诈骗名义代码 
                mTbBstFraudCollect.Telattribution = context.Request.Form["texTelattribution"]; //被举报号码归属省
                mTbBstFraudCollect.Msgcontent = context.Request.Form["texMsgcontent"]; //短信内容
                mTbBstFraudCollect.Informername = context.Request.Form["texInformername"]; //举报人姓名
                mTbBstFraudCollect.Informertel = context.Request.Form["texInformertel"]; //举报人电话
                mTbBstFraudCollect.Informercompany = context.Request.Form["texInformercompany"]; //举报人单位
                mTbBstFraudCollect.Createdate = DateTime.Now;
                mTbBstFraudCollect.Movesign = "1"; //同步标志
                mTbBstFraudCollect.Isextraction = "0"; //是否被内网民生警务抽取 0 未抽取 1 已抽取
                mTbBstFraudCollect.Fraudtypecontent = TbBstFraudCollectBLL.GetZPname(context.Request.Form["selFraudtype"]); //诈骗名义具体内容
                mTbBstFraudCollect.Remark = context.Request.Form["texRemark"]; //备注
                mTbBstFraudCollect.Ip = context.Request.Form["texIp"]; //举报人ip地址

                mResultModel = TbBstFraudCollectBLL.Insert(mTbBstFraudCollect, context.Session["UserID"], true); //新建操作
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 删除记录
        /// <summary>
        /// 删除记录
        /// </summary>
        protected void DeleteRecord()
        {

            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //声明返回类型
            try
            {
                string[] arryParamKey = context.Request.Params["id"].ToString().Split(',');
                if (arryParamKey.Length == 0)
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                TbBstFraudCollect mTbBstFraudCollect = new TbBstFraudCollect();
                foreach (var paramKey in arryParamKey)
                {
                    var FraudCollect = TbBstFraudCollectBLL.Load(paramKey);
                    if (FraudCollect.Isextraction == "0")
                    {
                        mResultModel = TbBstFraudCollectBLL.Delete(paramKey, context.Session["UserID"], true); //删除对应的信息
                    }
                    else
                    {
                        mResultModel.ErrorCode = -7777;
                        mResultModel.ISSucceed = false;
                        mResultModel.Message = "无法删除已审核的信息";
                    }
                   
                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); ; //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, ""); //写入异常日志
                mResultModel.ErrorCode = -9999; //-9999代码是系统异常通用代码
                mResultModel.ISSucceed = false; //行为执行失败
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }

        }
        #endregion

        #region 修改记录
        /// <summary>
        /// 修改记录
        /// </summary>
        protected void EditRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            TbBstFraudCollect mTbBstFraudCollect = new TbBstFraudCollect();

            string paramKey = context.Request.Params["id"];
            try
            {

                if (string.IsNullOrEmpty(paramKey))
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                mTbBstFraudCollect = TbBstFraudCollectBLL.Load(paramKey); //根据键值获取实体信息
                if (mTbBstFraudCollect != null)
                {
                    mTbBstFraudCollect.Receivedatetime = Convert.ToDateTime(context.Request.Form["texShowTime"]); //收到诈骗电话（短信）时间
                    mTbBstFraudCollect.Tel = context.Request.Form["texTel"]; //来电显示的号码
                    if (!string.IsNullOrEmpty(mTbBstFraudCollect.Tel))
                    {
                        string telNum = mTbBstFraudCollect.Tel.Substring(0, 3);
                        string carrieroperator = TbBstFraudCollectBLL.GetName(telNum);
                        //被举报号码所属运营商名称 来电显示号码为非手机号的名称为其他
                        if (!string.IsNullOrEmpty(carrieroperator))
                        {
                            mTbBstFraudCollect.Operatorsname = carrieroperator;
                        }
                        else
                        {
                            mTbBstFraudCollect.Operatorsname = "其他";
                        }
                        string carrieroperatorNum = "0";
                        switch (carrieroperator)
                        {
                            case "中国移动":
                                carrieroperatorNum = "1";
                                break;
                            case "中国联通":
                                carrieroperatorNum = "2";
                                break;
                            case "中国电信":
                                carrieroperatorNum = "3";
                                break;
                            case "":
                                carrieroperatorNum = "0";
                                break;
                            default:
                                break;
                        }
                        mTbBstFraudCollect.Operatorscode = carrieroperatorNum; //被举报号码所属运营商代码 1 移动号码 2 联通 3 电信 0 其他（来电显示号码为非手机号的）
                    }
                    mTbBstFraudCollect.Fraudtype = Convert.ToDecimal(context.Request.Form["selFraudtype"]); //诈骗名义代码
                    mTbBstFraudCollect.Telattribution = context.Request.Form["texTelattribution"]; //被举报号码归属省
                    mTbBstFraudCollect.Msgcontent = context.Request.Form["texMsgcontent"]; //短信内容
                    mTbBstFraudCollect.Informername = context.Request.Form["texInformername"]; //举报人姓名
                    mTbBstFraudCollect.Informertel = context.Request.Form["texInformertel"]; //举报人电话
                    mTbBstFraudCollect.Informercompany = context.Request.Form["texInformercompany"]; //举报人单位
                    mTbBstFraudCollect.Fraudtypecontent = TbBstFraudCollectBLL.GetZPname(context.Request.Form["selFraudtype"]); //诈骗名义具体内容
                    mTbBstFraudCollect.Remark = context.Request.Form["texRemark"]; //备注
                    mTbBstFraudCollect.Ip = context.Request.Form["texIp"]; //举报人ip地址

                    mResultModel = TbBstFraudCollectBLL.Update(mTbBstFraudCollect, context.Session["UserID"], true); //新建操作
                }
                else
                {
                    mResultModel.ErrorCode = -9990; //没有获取所需的信息，信息可能已被删除
                    mResultModel.ISSucceed = false;

                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 获取实体记录
        /// <summary>
        /// 获取实体记录
        /// </summary>
        protected void GetModel()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            Hashtable ht = new Hashtable();
            string paramKey = context.Request.Params["id"];
            string json = "";
            string dmt = "";
            try
            {
                TbBstFraudCollect mTbBstFraudCollect = new TbBstFraudCollect(); //声明实体
                if (!string.IsNullOrEmpty(paramKey))
                {
                    mTbBstFraudCollect = TbBstFraudCollectBLL.Load(paramKey); //根据键值获取管理员
                    if (mTbBstFraudCollect != null)
                    {
                        json = JsonConvert.SerializeObject(mTbBstFraudCollect);
                    }
                    if (!string.IsNullOrEmpty(mTbBstFraudCollect.Isextraction))
                    {
                        string isMov = mTbBstFraudCollect.Isextraction == "0" ? "未抽取" : "已抽取";
                        ht.Add("Isext", isMov);
                    }
                    if (!string.IsNullOrEmpty(mTbBstFraudCollect.Source))
                    {
                        string isMov = mTbBstFraudCollect.Source == "1" ? "来电诈骗" : "短信诈骗";
                        ht.Add("Sources", isMov);
                    }

                    if (mTbBstFraudCollect.Receivedatetime.ToString("yyyy") == "0001")
                    {
                        dmt = "无时间";
                        ht.Add("ShowTime", dmt);
                    }
                    else
                    {
                        dmt = mTbBstFraudCollect.Receivedatetime.ToString("yyyy-MM-dd HH:mm:ss");
                        ht.Add("ShowTime", dmt);
                    }
                    json = JsonString.AddAttribute(json, ht);
                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9990;
                mResultModel.ISSucceed = false;
                json = JsonConvert.SerializeObject(mResultModel);
            }
            context.Response.Write(json);
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取数据记录
        /// </summary>
        protected void QueryRecord()
        {
            TableMessage tm = new TableMessage();
            HearderMessage hm = new HearderMessage();
            List<HearderMessage> list = new List<HearderMessage>();
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            string pageSize = context.Request.Form["pageSize"];
            string pageIndex = context.Request.Form["pageIndex"];
            string Tel = context.Request.Form["texTel"];
            string ZpCode = context.Request.Form["selStatus"];
            string selOPERATORSCODE = context.Request.Form["selOPERATORSCODE"];
            string selSource = context.Request.Form["SelSource"];
            DateTime begintime = new DateTime();
            DateTime endtime = new DateTime();
            if (!string.IsNullOrEmpty(context.Request.Form["texTimeStart"]))
            {
                begintime = Convert.ToDateTime(context.Request.Form["texTimeStart"]);
            }
            if (!string.IsNullOrEmpty(context.Request.Form["texTimeEnd"]))
            {
                endtime = Convert.ToDateTime(context.Request.Form["texTimeEnd"]);
            }


            if (String.IsNullOrEmpty(pageSize) || String.IsNullOrEmpty(pageIndex) || !AnyString.IsNumeric(pageSize) || !AnyString.IsNumeric(pageIndex))
                return;
            DataTable dtTbBstFraudCollect = new DataTable();
            int intPageSize = Convert.ToInt32(pageSize);
            int intPageIndex = Convert.ToInt32(pageIndex);
            QueryByPage Q = new QueryByPage();
            QueryInfo qInfo = new QueryInfo();
            SqlConditionList sqllist = new SqlConditionList();
            if (!string.IsNullOrEmpty(Tel))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, TbBstFraudCollectConst.Columns.Tel, '%' + Tel + '%'), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(ZpCode))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, TbBstFraudCollectConst.Columns.Fraudtype, ZpCode), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(selOPERATORSCODE))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, TbBstFraudCollectConst.Columns.Operatorscode, selOPERATORSCODE), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(selSource))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, TbBstFraudCollectConst.Columns.Source, selSource), SqlLogicOper.AND_LOGIC);
            }
            if (begintime.ToString() != "0001/1/1 0:00:00")
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.NOT_LESS_THAN, TbBstFraudCollectConst.Columns.Receivedatetime, begintime), SqlLogicOper.AND_LOGIC);
            }
            if (endtime.ToString() != "0001/1/1 0:00:00")
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.NOT_MORE_THAN, TbBstFraudCollectConst.Columns.Receivedatetime, endtime), SqlLogicOper.AND_LOGIC);
            }
            sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.NOT_EQUAL, TbBstFraudCollectConst.Columns.Isextraction, "2"), SqlLogicOper.AND_LOGIC);
            dtTbBstFraudCollect = Q.GetRecordByPage("*", sqllist.ToSql(), "CREATEDATE", intPageSize, intPageIndex, TbBstFraudCollectConst.TableName, TbBstFraudCollectConst.FieldKey, "DESC").Tables[0];

            if (dtTbBstFraudCollect != null && dtTbBstFraudCollect.Rows.Count > 0)
            {
                for (int i = 0; i < dtTbBstFraudCollect.Rows.Count; i++)
                {
                    string color = "FF0000";
                    string mes = "已删除";
                    if (dtTbBstFraudCollect.Rows[i]["ISEXTRACTION"].ToString() == "0")  //未推送
                    {
                        color = "FF0000";
                        mes = "未审核";
                    }
                    else if (dtTbBstFraudCollect.Rows[i]["ISEXTRACTION"].ToString() == "1")  //推送
                    {
                        color = "32C904";
                        mes = "已审核";
                    }
                    else
                    {
                        mes = "已删除";  //已删除
                    }
                    dtTbBstFraudCollect.Rows[i]["ISEXTRACTION"] = "<span style=\"color:#" + color + "\">" + mes + "</span>";

                    if (!string.IsNullOrEmpty(dtTbBstFraudCollect.Rows[i]["SOURCE"].ToString()))
                    {
                        if (dtTbBstFraudCollect.Rows[i]["SOURCE"].ToString() == "1")
                        {
                            dtTbBstFraudCollect.Rows[i]["SOURCE"] = "来电";
                        }
                        else
                        {
                            dtTbBstFraudCollect.Rows[i]["SOURCE"] = "短信";
                        }
                    }
                        
                }
                for (int i = 0; i < dtTbBstFraudCollect.Columns.Count; i++)
                {
                    hm = new HearderMessage();
                    hm.name = dtTbBstFraudCollect.Columns[i].Caption;
                    list.Add(hm);
                }
                tm.Header = list;
                tm.data = dtTbBstFraudCollect;
                tm.totalCount = Convert.ToString((int)Q.RowCount);
            }
            IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
            string json = JsonConvert.SerializeObject(tm, dtConverter);
            context.Response.Write(json);
        }
        #endregion

        //审核
        public void ToExamine()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //声明返回类型
            try
            {
                string[] arryParamKey = context.Request.Params["id"].ToString().Split(',');
                if (arryParamKey.Length == 0)
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                foreach (var paramKey in arryParamKey)
                {
                    var FraudCollect = TbBstFraudCollectBLL.Load(paramKey);
                    if (FraudCollect != null)
                    {
                        FraudCollect.Isextraction = "1";
                        FraudCollect.Movesign = "0";
                        FraudCollect.Extractiondate = DateTime.Now;
                        mResultModel = TbBstFraudCollectBLL.Update(FraudCollect, "", false);
                    }
                    else
                    {
                        mResultModel.ErrorCode = -9990;
                        mResultModel.ISSucceed = false;
                    }
                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel));
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, ""); //写入异常日志
                mResultModel.ErrorCode = -9999; //-9999代码是系统异常通用代码
                mResultModel.ISSucceed = false; //行为执行失败
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }

        }

    }
}


